<?php
/**
 * 活动(activity)表管理
 * Created by PhpStorm.
 * User: deng
 * Date: 2016/7/15
 * Time: 15:00
 */
class PinkActivity extends AdminBase{

    protected $do;

    function __construct(){
        parent::__construct();
        $this->do = 'activityManage';
        $this->{$this->op}();
    }

    function indexAction(){
        include_once template("admin_v2/activity");
        exit();
    }

    function indexShopAction(){
        include_once template("admin_v2/shopbanner");
        exit();
    }

    function listAction(){
        $this->fetchData(array_merge([
            'do'     => $this->do,
            'format' => 'json',
            'operate' => 'list',
        ], $this->req));
    }

    function msgdetailAction(){
        $this->fetchData([
            'do'     => $this->do,
            'format' => 'json',
            'operate' => 'detail',
            'id' => $this->req['id']
        ]);
    }

    function editdetailAction(){
        $this->req['content'] = urlencode($this->req['content']);
        $this->req['action_url'] = urlencode($this->req['action_url']);
        $this->req['link_url'] = urlencode($this->req['link_url']);
        $this->req['start'] = $this->req['start'] ? strtotime($this->req['start']) : time();
        $this->req['end'] = $this->req['end'] ? strtotime($this->req['end']) : time()+(24*3600);
        $this->req = $this->getDiarySquare();

        $this->fetchData(array_merge([
            'do'     => $this->do,
            'format' => 'json',
            'operate' => 'edit',
        ], $this->req));
    }

    function addAction(){
        $start_time=strtotime(date('Y-m-d 00:00:00'));
        //默认开始时间
        $this->req['content'] = urlencode(preg_replace("/(\s+)/",'，',$this->req['content']));
        $this->req['name'] = preg_replace("/(\s+)/",'，',$this->req['name']);
        $this->req['title'] = preg_replace("/(\s+)/",'，',$this->req['title']);
        $this->req['action_url'] = urlencode($this->req['action_url']);
        $this->req['link_url'] = urlencode($this->req['link_url']);
//        $this->req['start'] = $this->req['start'] ? strtotime($this->req['start']) : time();
//        $this->req['end'] = $this->req['end'] ? strtotime($this->req['end']) : time()+(24*3600);
        if($this->req['start']){
            //如果开始时间选择

            $this->req['start']=strtotime($this->req['start']);
            $start_time1=strtotime(date('Y-m-d',$this->req['start']));
            //开始时间
            $this->req['end'] = $this->req['end'] ? strtotime($this->req['end']) : $start_time1+3600*23 + 3599;
        }else {
            $this->req['start']=$start_time;
            $this->req['end'] = $this->req['end'] ? strtotime($this->req['end']) : $start_time + 3600*23 + 3599;
        }
            if ($this->req['type'] == 'wisdom'){//每日一语处理
                $this->req['start'] = strtotime( date('Y-m-d 00:00:00', $this->req['start']) );
                $this->req['end'] = $this->req['start'] + 3600*23 + 3599;
            }

        $this->req = $this->getDiarySquare();

        $this->fetchData(array_merge([
            'do'     => $this->do,
            'format' => 'json',
            'operate' => 'add',
        ], $this->req));
    }

    function deleteAction(){
        $this->fetchData([
            'do'     => $this->do,
            'format' => 'json',
            'operate' => 'delete',
            'id' => $this->req['id']
        ]);
    }

    function flushdayAction(){
        $flush_day = $this->req['dates'] ? trim($this->req['dates']) : '';
        if (empty($flush_day))return $this->errorReturn('参数错误');
        $url = "http://tips.ffrj.net/tips/create?ymd=".$flush_day;
//     $url = "http://tips.ffrj.net/tips/create?ymd=20151106";
        $json = getHttpResponsePOST($url);
        $result = parseData($json);

        $res = 0;
        if (!empty($result['info']['url']) && !empty($result['info']['image_url'])) {
            $res = 1;
            $this->resultReturn();
        }
        $this->errorReturn('操作失败');
    }

    function getDiarySquare(){
        if (substr(urldecode($this->req['action_url']), 0, 21) === 'pinksns://diary/info?' && $this->req['type'] == 'recommend_banner') {
            parse_str(urldecode($this->req['action_url']), $output);
            $diary = requestApi(array(
                'do' => 'getDiarySquareInfo',
                'format'    => 'json',
                'bodyId'  => $output['bodyId']
            ));

            if ($diary && $diary['detail']['user']['verified']) {
                $tmp = array(
                    'is_origin' => $diary['detail']['original'] == 1 ? '1' : '0',
                    'user' => array(
                        'uid' => strval($diary['detail']['user']['uid']),
                        'nickname' => strval($diary['detail']['user']['nickname']),
                        'is_verified' => strval($diary['detail']['user']['verified'])
                    )
                );
                $this->req['extend'] = var_json_encode(array('data' => $tmp));
            }
        }elseif (substr(urldecode($this->req['action_url']), 0, 23) === 'pinksns://article/info?' && $this->req['type'] == 'recommend_banner'){
            parse_str(urldecode($this->req['action_url']), $output);
            $uid = $output['author_uid'];
            $user = requestApi(array(
                'do'     => 'getUserInfo',
                'format' => 'json',
                'uid'    => $uid
            ));

            $tmp = array(
                'is_origin' => '1',
                'user' => array(
                    'uid' => strval($uid),
                    'nickname' => strval($user['userInfo']['nickname']),
                    'is_verified' => strval($user['userInfo']['verified'])
                )
            );
            $this->req['extend'] = var_json_encode(array('data' => $tmp));
        }

        return $this->req;
    }

    //根据bodyId获取数据
    function getDiaryInfoByBodyIdAction(){
        $this->fetchData([
            'do'     => 'getDiaryInfoByBodyId',
            'format' => 'json',
            'bodyId' => $this->req['bodyId'],
        ]);
    }
    //根据gid获取数据
    function getDiaryTopicInfoAction () {
        $this->fetchData([
            'do'     => 'getDiaryTopicInfo',
            'format' => 'json',
            'topic_id' => $this->req['gid'],
        ]);
    }
    //根据gid获取圈子数据
    function getGroupInfoAction() {
        $this->fetchData([
            'do'     => 'group',
            'format' => 'json',
            'action' => 'getGroupInfo',
            'gid' => $this->req['gid'],
        ]);
    }
    //根据标签名获取数据
    function searchDiaryTopicAction () {
        $this->fetchData([
            'do'     => 'searchDiaryTopic',
            'format' => 'json',
            'name' => $this->req['name'],
        ]);
    }

}

new PinkActivity;